package com.wsoft.portal.manager;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.RedisService;
import com.wsoft.portal.entity.PortalModuleSubEntity;
import com.wsoft.portal.mapper.PortalModuleSubMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author z001
 * @Date 2024/12/27 10:09
 */
@Service
public class PortalModuleSubManager extends ServiceImpl<PortalModuleSubMapper, PortalModuleSubEntity> {

    @Resource
    private RedisService redisService;

    @Override
    public boolean updateById(PortalModuleSubEntity entity){
        boolean b = super.updateById(entity);
        if(b){
            redisService.del(RedisKeyConstant.Portal.MODULE_SUB_ID + entity.getId());
            redisService.del(RedisKeyConstant.Portal.MODULE_SUB_LIST + entity.getPortalModuleId());
        }
        return b;
    }

    @GetRedisCache(type = RedisKeyConstant.Portal.MODULE_SUB_ID,
            expire_sec = RedisKeyConstant.Portal.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public PortalModuleSubEntity getById(Long id){
        return super.getById(id);
    }

    /**
     * 批量保存
     * @param list
     * @return
     */
    public boolean saveBatch(List<PortalModuleSubEntity> list){
        boolean b = super.saveBatch(list);
        if(b){
            redisService.deleteByKeyPattern(RedisKeyConstant.Portal.MODULE_SUB_LIST + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }

    /**
     * 保存
     * @param entity
     * @return
     */
    @Override
    public boolean save(PortalModuleSubEntity entity){
        boolean b = super.save(entity);
        if(b){
            redisService.deleteByKeyPattern(RedisKeyConstant.Portal.MODULE_SUB_LIST + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }

    /**
     * 根据父id查询
     * @param parIdList
     * @return
     */
    public List<PortalModuleSubEntity> getParIdList(List<Long> parIdList){
        if(CollUtil.isEmpty(parIdList)){
            return new ArrayList<>();
        }
        List<PortalModuleSubEntity> resultList = new ArrayList<>();
        for (Long parId : parIdList) {
            List<PortalModuleSubEntity> list = getParId(parId);
            resultList.addAll(list);
        }
        return resultList;
    }

    @GetRedisCache(type = RedisKeyConstant.Portal.MODULE_SUB_LIST,
            expire_sec = RedisKeyConstant.Portal.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public List<PortalModuleSubEntity> getParId(Long  parId){
        QueryWrapper<PortalModuleSubEntity> query = new QueryWrapper<>();
        query.lambda().eq(PortalModuleSubEntity::getPortalModuleId,parId);
        return list(query);
    }

    /**
     * 根据id移除
     * @param id
     * @return
     */
    public boolean removeById(Long id){
        PortalModuleSubEntity entity = getById(id);
        boolean b = super.removeById(id);
        if(b){
            redisService.del(RedisKeyConstant.Portal.MODULE_SUB_ID + id);
            redisService.del(RedisKeyConstant.Portal.MODULE_SUB_ID + entity.getPortalModuleId());
        }
        return b;
    }

    /**
     * 根据 PortalModuleId 移除
     * @param moduleId
     * @return
     */
    public boolean removeByModuleId(Long moduleId){
        QueryWrapper<PortalModuleSubEntity> query = new QueryWrapper<>();
        query.lambda().eq(PortalModuleSubEntity::getPortalModuleId,moduleId);
        boolean b = remove(query);
        if(b){
            redisService.deleteByKeyPattern(RedisKeyConstant.Portal.MODULE_SUB + RedisKeyConstant.PATTERN_SYMBOL);
            redisService.deleteByKeyPattern(RedisKeyConstant.Portal.MODULE_SUB_LIST + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }
}
